package com.itheima.controller;

import com.itheima.annotion.WebLog;
import com.itheima.common.response.ResponseResult;
import com.itheima.common.response.StatusCode;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者 itcast
 * @创建日期 2020/10/13 10:25
 **/
@RestController   // controller  responseBody
@RequestMapping("user")
public class UserController {
    // 统一异常处理
    // @ControllerAdvice
    // @ExceptionHandler(Exception.class)
    @Autowired
    UserService userService;
    // GET  http://localhost:9001/user  查询全部用户
    @GetMapping
    public ResponseResult findAll(){
        return ResponseResult.okResult(userService.findAll());
    }
    // POST  http://localhost:9001/user  添加用户
    @WebLog("添加用户")
    @PostMapping
    public ResponseResult insert(@RequestBody User user){
        boolean insert = userService.insert(user);
        if(insert){
            return ResponseResult.okResult(null);
        }else {
            return ResponseResult.errorResult(StatusCode.USER_ADD_ERROR);
        }
    }
}
